<?php
/**
 * 菜单列表
 * User: Administrator
 * Date: 2018\9\18 0018
 * Time: 17:16
 */

namespace app\Lib\Model\AdminAuth\AdminMenu;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\AdminAuth\AdminMenu\GetMenuListHandle;
use app\Lib\Model\AdminAuth\MenuAuth;
use app\Lib\Model\DbModel\AdminMenu;

class GetMenuList implements GetMenuListHandle
{
    private  $keyword;
    private  $keywordType;
    private  $page;
    private  $limit;
    private  $table;
    private  $isPage;
    /**
     * 搜索条件
     * @param $keyword
     * @return $this
     */
    public function setKeyword($keyword)
    {
        $this->keyword = trim($keyword);
        return $this;
    }

    /**
     * 搜索类型
     * @param $keywordType
     * @return $this
     */
    public function setKeywordType($keywordType)
    {
        $this->keywordType = trim($keywordType);
        return $this;
    }

    /**
     * 设置页数
     * @param int $page
     * @return $this
     */
    public function setPage($page = 1)
    {
        $this->page = intval($page);
        return $this;
    }

    /**
     * 设置行数
     * @param int $limit
     * @return $this
     */
    public function setLimit($limit = 10)
    {
        $this->limit = intval($limit);
        return $this;
    }

    public function setIsPage($isPage){
        $this->isPage = $isPage;
        return $this;
    }

    /**
     * 执行代码
     * @return array
     * @throws BlException
     */
    public function run()
    {
        $model =  $this->getSelectObj();
        $count =  $model->count();
        if($count<=0){
            return ['count'=>0,'list'=>[]];
        }
        if($this->isPage){
            return $count;
        }
        $model =  $this->getSelectObj();
        $list  =  $model->limit(($this->page-1)*$this->limit,$this->limit)->column('id');
        $list  =  $this->handleArray($list);
        return ['count'=>$count,'list'=>$list];
    }

    /**
     * 实例化权限类填满数组
     * @param $list
     * @return array
     * @throws BlException
     */
    private function handleArray($list){
        if(empty($list)){
            throw new BlException('暂无数据');
        }
        $model = $this->getTable();
        $data  = [];
        foreach ($list as $v){
            $menuAuth = (new MenuAuth($v));
            $array    = [
                $model->_id=>$menuAuth->getMenuId(),
                $model->_name=>$menuAuth->getName(),
                $model->_project=>$menuAuth->getProject(),
                $model->_a=>$menuAuth->getA(),
                $model->_c=>$menuAuth->getC()
            ];
            $data[$v] = $array;
        }
        return $data;
    }
    /**
     * 查询条件
     * @return array
     */
    private function getMap(){
        $model  = $this->getTable();
        $map    = [];
        if(!empty($this->keywordType)&& !empty($this->keyword)){
            switch ($this->keywordType){
                case $model->_project:
                    $map[$model->_project]  = $this->keyword;
                    break;
                case $model->_a:
                    $map[$model->_a]        = $this->keyword;
                    break;
                case $model->_c:
                    $map[$model->_c]        = $this->keyword;
                    break;
                case $model->_name:
                    $map[$model->_name]     = ['like','%'.$this->keyword.'%'];
                    break;
            }
        }
        return $map;
    }

    /**
     * 查询对象
     * @return AdminMenu
     */
    private function getSelectObj(){
        $map    = $this->getMap();
        $model  = $this->getTable();
        $result = $model->where($map);
        return $result;
    }

    /**
     * 数据表
     * @return AdminMenu
     */
    private function getTable(){
        if(empty($this->table)){
            $this->table = (new AdminMenu());
        }
        return $this->table;
    }

}